package sige

import org.springframework.dao.DataIntegrityViolationException

class GrupoController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    
    static permissoes = ["index", "list", "create", "save", "show", "edit", "update", "delete"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [grupoInstanceList: Grupo.list(params), grupoInstanceTotal: Grupo.count()]
    }

    def create() {
        def c = []
        def grupo = Grupo.get(session.grupo.id)      
        [grupoInstance: new Grupo(params), contains: c, totalList: grupo.permissoes]
    }

    def save() {
        def grupoInstance = new Grupo(params)
        (params.'permissoes.id').each{
            def p = Permissoes.get(it)
            grupoInstance.addToPermissoes(p)
        }
        if (!grupoInstance.save(flush: true)) {
            render(view: "create", model: [grupoInstance: grupoInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'grupo.label', default: 'Grupo'), grupoInstance.id])
        redirect(action: "show", id: grupoInstance.id)
    }

    def show(Long id) {
        def grupoInstance = Grupo.get(id)
        if (!grupoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'grupo.label', default: 'Grupo'), id])
            redirect(action: "list")
            return
        }

        [grupoInstance: grupoInstance]
    }

    def edit(Long id) {
        def grupoInstance = Grupo.get(id)
        if (!grupoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'grupo.label', default: 'Grupo'), id])
            redirect(action: "list")
            return
        }

        def grupo = Grupo.get(session.grupo.id)      
        [grupoInstance: grupoInstance, contains: grupoInstance.permissoes, totalList: grupo.permissoes]
    }

    def update(Long id, Long version) {
        def grupoInstance = Grupo.get(id)
        if (!grupoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'grupo.label', default: 'Grupo'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (grupoInstance.version > version) {
                grupoInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'grupo.label', default: 'Grupo')] as Object[],
                          "Another user has updated this Grupo while you were editing")
                render(view: "edit", model: [grupoInstance: grupoInstance])
                return
            }
        }
        
        def perms = []
        (params.'permissoes.id').each(){
            perms += Permissoes.get(it)
        }
        grupoInstance?.permissoes = perms

        grupoInstance.properties = params

        if (!grupoInstance.save(flush: true)) {
            render(view: "edit", model: [grupoInstance: grupoInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'grupo.label', default: 'Grupo'), grupoInstance.id])
        redirect(action: "show", id: grupoInstance.id)
    }

    def delete(Long id) {
        def grupoInstance = Grupo.get(id)
        if (!grupoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'grupo.label', default: 'Grupo'), id])
            redirect(action: "list")
            return
        }

        try {
            grupoInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'grupo.label', default: 'Grupo'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'grupo.label', default: 'Grupo'), id])
            redirect(action: "show", id: id)
        }
    }
    
    def addPermissao(Long id){
        def Permissao = Permissoes.get(id)
        def lista = []
        def cont = sTl(params?.contains)
        cont.each{
            lista += Permissoes.get(it)
        }
        
        def total = []
        cont = sTl(params?.totalList)
        cont.each{
            total += Permissoes.get(it)
        }
        
        lista += Permissao
        render ( template: 'objectSelect', model:['contains':lista, 'totalList': total - lista] )
    }
    
    def delPermissao(Long id){
        def Permissao = Permissoes.get(id)
        def lista = []
        def cont = sTl(params?.contains)
        cont.each{
            lista += Permissoes.get(it)
        }
        
        def total = []
        cont = sTl(params?.totalList)
        cont.each{
            total += Permissoes.get(it)
        }
        
        lista -= Permissao
        render ( template: 'objectSelect', model:['contains':lista, 'totalList': total - lista] )
    }
    
    List sTl(String s){
        s = s - '['
        s = s - ']'
        def l = []
        if( s.size() > 0 ){
            l = s.split(',').collect{it as int}
        }
        l
    }
}
